package com.jxwifi.cloud.quickclean.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.basicactivity.BasicFrgment;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.adapter.QucikListAdapter;
import com.jxwifi.cloud.quickclean.app.StateUtils;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.bean.InRecordBean;
import com.jxwifi.cloud.quickclean.bean.PageVoBean;
import com.jxwifi.cloud.quickclean.bean.QuickRecordBean;
import com.jxwifi.cloud.quickclean.bean.QuickTreBean;
import com.jxwifi.cloud.quickclean.model.AuzImpl;
import com.jxwifi.cloud.quickclean.okhttp.AuzHttp;
import com.jxwifi.cloud.quickclean.okhttp.DataJson_Cb;
import com.jxwifi.cloud.quickclean.okhttp.OkHttp;
import com.jxwifi.cloud.quickclean.okhttp.Params;
import com.jxwifi.cloud.quickclean.pay.PayResult;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class QuickListFragment extends BasicFrgment {

    @Bind(R.id.rv_fg_quick_list)
    RecyclerView mRvFgQuickList;

    private QucikListAdapter mAdapter;

    private String ProductPriceCode = "";
    private InRecordBean inoutRecord;

    private int intotal;
    private int inCurrentCounter;
    private boolean inisErr = true;

    private int outtotal;
    private int outCurrentCounter;
    private boolean outisErr = true;
    private int indexPage=2;

    private View notDataView;


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fg_quick_listview, container, false);
        ButterKnife.bind(this, root);
        openEventBus();
        ProductPriceCode = getArguments().getString("product_price_code".toLowerCase());
        inoutRecord= (InRecordBean) getArguments().getSerializable("inrecordbean");
        initView();
        return root;
    }

    public void initView() {
        if (StateUtils.QUICK_RECHARGE.equals(ProductPriceCode)){
            intotal=inoutRecord.getTotal();
            inCurrentCounter=inoutRecord.getSize();
        }else{
            outtotal=inoutRecord.getTotal();
            outCurrentCounter=inoutRecord.getSize();
        }
        mRvFgQuickList.setLayoutManager(new LinearLayoutManager(mBaseContext));
        mRvFgQuickList.setAdapter(mAdapter = new QucikListAdapter(R.layout.list_fg_quick_item, ProductPriceCode));
        mAdapter.openLoadAnimation();
        mAdapter.openLoadAnimation(BaseQuickAdapter.FOOTER_VIEW);
        if (inoutRecord.getRecords().size()>0){
            mAdapter.setNewData(inoutRecord.getRecords());
        }else{
            mAdapter.setEmptyView(getViews());
        }

        //上拉加载更多操作
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mRvFgQuickList.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (StateUtils.QUICK_RECHARGE.equals(ProductPriceCode)) { //充值记录
                            if (inCurrentCounter >= intotal) {
                                //数据全部加载完毕
                                mAdapter.loadMoreEnd();
                            } else {
                                if (inisErr) {
                                    initData();
                                    //成功获取更多数据
                                    outCurrentCounter = mAdapter.getData().size();
                                    mAdapter.loadMoreComplete();
                                } else {
                                    //获取更多数据失败
                                    inisErr = true;
                                    Toast.makeText(mBaseContext, "获取更多数据失败", Toast.LENGTH_LONG).show();
                                    mAdapter.loadMoreFail();
                                }
                            }
                        }else{  //消费记录
                            if (outCurrentCounter >= outtotal) {
                                //数据全部加载完毕
                                mAdapter.loadMoreEnd();
                            } else {
                                if (outisErr) {
                                    initData();
                                    //成功获取更多数据
                                    outCurrentCounter = mAdapter.getData().size();
                                    mAdapter.loadMoreComplete();
                                } else {
                                    //获取更多数据失败
                                    outisErr = true;
                                    Toast.makeText(mBaseContext, "获取更多数据失败", Toast.LENGTH_LONG).show();
                                    mAdapter.loadMoreFail();
                                }
                            }
                        }
                    }

                }, 1500);

            }
        }, mRvFgQuickList);
    }


    public void initData() {
        PageVoBean mBean = new PageVoBean();
        mBean.setPage(indexPage);
        mBean.setLimit(10);
        Params params = new Params();
        params.add("pageVo", mBean);
        params.add("type", "NONE");
        OkHttp.post(UrlPool.POST_BANK_LOOKUP, params, new DataJson_Cb() {
            @Override
            public void onSuccess(int code, String data) {
                QuickTreBean mBean = JSON.parseObject(data, QuickTreBean.class);
                EventBus.getDefault().post(mBean.getUcCustomerBank().getSurplusAmount(),"e-QuickResult");
                if (StateUtils.QUICK_RECHARGE.equals(ProductPriceCode)) { //充值记录
                    setData(false,mBean.getInRecord().getRecords());
                }else{
                    setData(false,mBean.getOutRecord().getRecords());
                }
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                outisErr = false;
                inisErr = false;
                Toast toast = Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
                ToastUtils.showMyToast(toast, 3000);
            }
        },TAG);
    }

    public void setData(boolean isRefresh, List data) {
        indexPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
           /* if (data == null || data.size() == 0) {
                //mNoData = false;
                mAdapter.setEmptyView(notDataView);
            } else {
                mAdapter.setNewData(data);
            }*/
        } else {
            if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (StateUtils.QUICK_RECHARGE.equals(ProductPriceCode)) {
            inCurrentCounter = mAdapter.getData().size();

        }else{
            outCurrentCounter = mAdapter.getData().size();
        }
        if (size < 10) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd(isRefresh);
        } else {
            mAdapter.loadMoreComplete();
        }
    }

    public View getViews() {
         View notDataView = getLayoutInflater().inflate(R.layout.qt_null_layout, (ViewGroup) mRvFgQuickList.getParent(), false);
        return notDataView;
    }
}
